package top.cdmrx.demo.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import top.cdmrx.common.R;
import top.cdmrx.demo.edu.entity.Teacher;
import top.cdmrx.demo.edu.entity.vo.TeacherQuery;
import top.cdmrx.demo.edu.service.TeacherService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @ApiModelProperty(value = "讲师查询")
    @GetMapping(value = "/findAll")
    public R findAllTeacher(){

        List<Teacher> list = teacherService.list(null);
        return R.success().data("item",list);
    }

    @ApiModelProperty(value = "逻辑讲师删除")
    @DeleteMapping(value = "deleted/{id}")
    public R deleteTeacher(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id){

        boolean b = teacherService.removeById(id);
        if (b){

            return R.success();
        }else {
            return R.fail();
        }
    }


    @ApiModelProperty(value = "分页请求")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageList(@PathVariable long current,
                      @PathVariable long limit){

        //创建page对象
        Page teacherPage = new Page<>(current, limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        teacherService.page(teacherPage, null);

        long total = teacherPage.getTotal();
        List records = teacherPage.getRecords();

        //第一种方法
//        return R.success().data("tatal",total).data("records",records);

        //第二种方法
        Map<String ,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("records",records);

        return R.success().data(map);
    }

    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

        //创建page独象
        Page<Teacher> objectPage = new Page<>(current, limit);

        //构建条件
        QueryWrapper<Teacher> teacherQueryQueryWrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            teacherQueryQueryWrapper.like("name",name);
        }

        if (!StringUtils.isEmpty(level)){
            teacherQueryQueryWrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(begin)){
            teacherQueryQueryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            teacherQueryQueryWrapper.le("gmt_modified",end);
        }

        //添加按加入时间降序排列
        teacherQueryQueryWrapper.orderByDesc("gmt_create");

        teacherService.page(objectPage,teacherQueryQueryWrapper);
        long total = objectPage.getTotal();
        List<Teacher> records = objectPage.getRecords();

        return R.success().data("total",total).data("rows",records);

    }

    @ApiModelProperty(value = "添加讲师ID")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return R.success();
        }else {
            return R.fail();
        }

    }

    @ApiModelProperty(value = "获取指定id的对象")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        Teacher byId = teacherService.getById(id);
        return R.success().data("teacher",byId);
    }

    /**
     * 修改讲师功能
     * @return
     */
    @ApiModelProperty(value = "修改讲师功能")
    @PostMapping
    public R updateTeacher(@RequestBody Teacher teacher){

        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.success();
        }else {

            return R.fail();
        }
    }
}

